package org.scratchcrew.web.jsf;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

public final class ContextUtils {
	private ContextUtils() {

	}

	public static String contextAddress(ServletContext servletContext,
			HttpServletRequest httpServletRequest) {
		String contextPath = servletContext.getContextPath() + '/';
		String serverName = httpServletRequest.getServerName();
		String requestUrl = httpServletRequest.getRequestURL().toString();
		int serverNameEndIndex = requestUrl.indexOf(serverName)
				+ serverName.length();
		int contextAdressEndIndex = requestUrl.indexOf(contextPath,
				serverNameEndIndex)
				+ contextPath.length();
		String contextAddress = requestUrl.substring(0, contextAdressEndIndex);
		return contextAddress;
	}

}
